package bill.study.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;

public class BlockingOrNot {

    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Kuang");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Chang");
        button.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("I cliked the button from " + Thread.currentThread().getName()+ " with " + Thread.currentThread().getId());
				System.out.println("+++++++++++++ inside listener the swing thing++++++++++++++++");
				threadStatistics();
			}});
        frame.getContentPane().add(button);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        threadStatistics();
    }
    
    private static void threadStatistics(){
    	Map<Thread, StackTraceElement[]> map =Thread.getAllStackTraces();
		System.out.println(map.keySet());
		Set<Thread> keySet = map.keySet();
		System.out.println("******How many threads with stack trace******");
		for(Thread t : keySet){
			System.out.println("Thread Name: " + t.getName() + " Id: " + t.getId());
		}
	
		System.out.println("Active count in the group: " + Thread.activeCount());
		
		Thread [] ts = new Thread[100];

		Thread.enumerate(ts);
		System.out.println("********how many threads in the group*************");
		for(Thread t: ts){
			if(t != null)
			System.out.println("Thread Name: " + t.getName() + " Id: " + t.getId());
		}
		System.out.println("********************************");

    }

    public static void main(String[] args) {     
    	System.out.println("+++++++++++++before constructing the swing thing++++++++++++++++");
    	threadStatistics();
    	createAndShowGUI();
    
        System.out.println("IO  from " + Thread.currentThread().getName()+ " with " + Thread.currentThread().getId());
    }
}
